<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="面经详情"
    />
    <header class="header">
      <h1>{{ DetailAobj.stem }}</h1>
      <p>
        {{ DetailAobj.createdAt }} | {{ DetailAobj.views }} 浏览量 |
        {{ DetailAobj.likeCount }} 点赞数
      </p>
      <p>
        <img :src="DetailAobj.avatar" alt="" />
        <span> {{ DetailAobj.creator }}</span>
      </p>
    </header>
    <main class="body">
      <div v-html="DetailAobj.content"></div>
    </main>
    <div class="opt">
      <van-icon
        :class="{ active: DetailAobj.likeFlag == 1 ? true : false }"
        @click="likeflag"
        name="like-o"
      />
      <van-icon
        :class="{ active: DetailAobj.collectFlag == 1 ? true : false }"
        @click="shouchang"
        name="star-o"
      />
    </div>
  </div>
</template>
<script>
import { DetailAPI, likeAPI } from '../api/detail.js'
export default {
  name: '',
  data () {
    return {
      DetailAobj: {}

    }
  },
  created () {
    this.getDetailAPI()
  },
  computed: {
  },
  methods: {
    //点赞
    async likeflag () {
      const res = await likeAPI({
        id: this.$route.params.id,
        optType: 1
      })
      console.log(res);

      if (this.DetailAobj.likeFlag > 0) {
        this.DetailAobj.likeCount--
      } else {
        this.DetailAobj.likeCount++
      }
      this.DetailAobj.likeFlag = this.DetailAobj.likeFlag == 0 ? 1 : 0

    },


    //收藏
    async shouchang () {
      console.log(1);
      const res = await likeAPI({
        id: this.$route.params.id,
        optType: 2
      })
      console.log(res);
      this.DetailAobj.collectFlag = this.DetailAobj.collectFlag == 0 ? 1 : 0
    },





    //获取数据
    async getDetailAPI () {
      const res = await DetailAPI(this.$route.params.id)
      console.log(res);
      this.DetailAobj = res.data
    }
  }
}
</script>
<style lang='less'  scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>
